package com.angus.modules.content.mapper;

import org.springframework.stereotype.Component;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

@Component
public class CourseLockMapper {
    private final ConcurrentHashMap<String, Lock> locks = new ConcurrentHashMap<>();

    public Lock getLock(String courseCode) {
        return locks.computeIfAbsent(courseCode, k -> new ReentrantLock());
    }

    // 可选：定期清理不再使用的锁（防止内存泄漏）
    public void cleanupLocks() {
        locks.entrySet().removeIf(entry -> !((ReentrantLock) entry.getValue()).isLocked());
    }
}